package com.cghs.ac.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.telephony.TelephonyManager;
import android.util.Log;

public class NetworkUtils {
	private static final String LOG_TAG = "NetworkUtils"; 
	
	/** unknown **/
	public static final String NETWORK_TYPE_UNKNOWN = "Unknown";
	public static final String NETWORK_TYPE_WIFI 	= "WIFI";
	public static final String NETWORK_TYPE_2G		= "2G";
	public static final String NETWORK_TYPE_3G 		= "3G";
	public static final String NETWORK_TYPE_4G 		= "4G";
	
	/**
	 * check whether the network is available.
	 * @param context
	 * @return true or false
	 */
	
	public static boolean isNetworkAvaliable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivityManager != null) {
			NetworkInfo[] networkInfos = connectivityManager
					.getAllNetworkInfo();
			for (int i = 0; i < networkInfos.length; i++) {
				State state = networkInfos[i].getState();
				if (NetworkInfo.State.CONNECTED == state) {
					return true;
				}
			}
		}
		return false;
	}
	
	
	/**
	 * Get the network type.
	 * 
	 * @param context
	 * @return 
	 * <li>NETWORK_TYPE_WIFI</li>
	 * <li>NETWORK_TYPE_2G</li>
	 * <li>NETWORK_TYPE_3G</li>
	 * <li>NETWORK_TYPE_4G</li>
	 * <li>NETWORK_TYPE_UNKNOWN</li>
	 */
	public static String getNetType(Context c) {
		ConnectivityManager connectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null) {
			return NETWORK_TYPE_UNKNOWN;
		}
		
		if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
			return NETWORK_TYPE_WIFI;
		} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
			switch (networkInfo.getSubtype()) {
				case TelephonyManager.NETWORK_TYPE_GPRS : // 联通2g
				case TelephonyManager.NETWORK_TYPE_CDMA : // 电信2g
				case TelephonyManager.NETWORK_TYPE_EDGE : // 移动2g
				case TelephonyManager.NETWORK_TYPE_1xRTT :
				case TelephonyManager.NETWORK_TYPE_IDEN :
					return NETWORK_TYPE_2G;
				case TelephonyManager.NETWORK_TYPE_EVDO_A : // 电信3g
				case TelephonyManager.NETWORK_TYPE_UMTS :
				case TelephonyManager.NETWORK_TYPE_EVDO_0 :
				case TelephonyManager.NETWORK_TYPE_HSDPA :
				case TelephonyManager.NETWORK_TYPE_HSUPA :
				case TelephonyManager.NETWORK_TYPE_HSPA :
				case TelephonyManager.NETWORK_TYPE_EVDO_B :
				case TelephonyManager.NETWORK_TYPE_EHRPD :
				case TelephonyManager.NETWORK_TYPE_HSPAP :
					return NETWORK_TYPE_3G;
				case TelephonyManager.NETWORK_TYPE_LTE :
					return NETWORK_TYPE_4G;
				default :
					if (networkInfo.getSubtypeName().equals("TD-SCDMA")
							|| networkInfo.getSubtypeName().equals("WCDMA")
							|| networkInfo.getSubtypeName().equals("W-CDMA")
							|| networkInfo.getSubtypeName().equals("CDMA2000")) {
						return NETWORK_TYPE_3G;
					}
					Log.e(LOG_TAG,
							"unknow network SubtypeName:"
									+ networkInfo.getSubtypeName());
					return NETWORK_TYPE_UNKNOWN;
			}
		}

		return NETWORK_TYPE_UNKNOWN;
	}
	
	
}
